//
//  GoodsViewController.m
//  TextApplication
//
//  Created by dllo on 16/9/5.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "GoodsViewController.h"
#import "ZWYNetTool.h"
#import "GoodsModel.h"
#import "TAMacro.h"
#import "UIImageView+WebCache.h"
#import "CommentModel.h"
#import "CommentTableViewCell.h"

#import "MJRefresh.h"
#import "TextView.h"

@interface GoodsViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *goodImageView;
@property (nonatomic, strong) UILabel *goodNameLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIImageView *perImageView;
@property (nonatomic, strong) UILabel *perNameLabel;

@property (nonatomic, strong) UITableView *commentTableView;
@property (nonatomic, strong) NSMutableArray *commentArray;

@property (nonatomic, assign) NSInteger page;

@end

@implementation GoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.title = @"良品";
    self.page = 1;
    [self createBackButton];
    [self createHeadView];
    [self createCommentTableView];
    [self createData];
    [self createCommentData];
}

- (void)createCommentTableView {
    self.commentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 108) style:UITableViewStylePlain];
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    [self.view addSubview:self.commentTableView];
    self.commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.commentTableView.backgroundColor = [UIColor colorWithWhite:0.227 alpha:1.000];
    self.commentTableView.tableHeaderView = self.backView;
    self.commentTableView.rowHeight = 120;
    [self.commentTableView registerClass:[CommentTableViewCell class] forCellReuseIdentifier:@"commentReuse"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self upLoad];
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentReuse" forIndexPath:indexPath];
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [self.commentArray[indexPath.row] user_image]]]];
    cell.userNameLabel.text = [self.commentArray[indexPath.row] user_name];
    cell.commentLabel.text = [self.commentArray[indexPath.row] msg];
    cell.dataLabel.text = [self.commentArray[indexPath.row] create_time];
    
    return cell;
}
- (void)createHeadView {
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH + 180)];
    self.backView.backgroundColor = [UIColor colorWithWhite:0.227 alpha:1.000];
//    [self.view addSubview:self.backView];
    
    self.goodImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, WIDTH - 20)];
    [self.backView addSubview:self.goodImageView];
    
    self.goodNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5 + WIDTH - 10 + 10, WIDTH - 10, 30)];
    [self.backView addSubview:self.goodNameLabel];
    
    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, WIDTH + 5 + 30 + 10, WIDTH - 10, 30)];
    [self.backView addSubview:self.priceLabel];
    
    self.perImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, WIDTH + 75 + 10, 70, 70)];
    [self.backView addSubview:self.perImageView];
    
    self.perNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, WIDTH + 75 + 25, 200, 30)];
    [self.backView addSubview:self.perNameLabel];
    
    self.goodNameLabel.textColor = [UIColor whiteColor];
    self.priceLabel.textColor = [UIColor colorWithRed:0.178 green:0.347 blue:0.621 alpha:1.000];
    self.perNameLabel.textColor = [UIColor colorWithRed:0.178 green:0.455 blue:0.810 alpha:1.000];
    
}

- (void)createData {
    self.dataArray = [NSMutableArray array];
    NSString *str1 = @"http://mobile.iliangcang.com/goods/goodsDetail?a=b&goods_id=";
    NSString *str2 = @"&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
    NSString *str = [[str1 stringByAppendingString:self.good_id] stringByAppendingString:str2];
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};

    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSDictionary *dic = result[@"data"][@"items"];
        GoodsModel *good = [GoodsModel modelWithDic:dic];
        [self.dataArray addObject:good];
        NSLog(@"%@", self.dataArray);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.goodImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [self.dataArray[0] goods_image]]]];
            self.goodNameLabel.text = [self.dataArray[0] goods_name];
            self.priceLabel.text = [self.dataArray[0] price];
           
            [self.perImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [self.dataArray[0] headimg]]]];
            self.perNameLabel.text = [self.dataArray[0] owner_name];
        });
        
    } andFailureBlock:^(NSError *error) {
    }];
}

- (void)upLoad {
     self.commentTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(createCommentData)];
}

- (void)createCommentData {
    if (self.page == 1) {
    self.commentArray = [NSMutableArray array];
    }
    NSString *str1 = @"http://mobile.iliangcang.com/comments/goods?a=b&goods_id=";
    NSString *str2 = @"&page=";
    NSString *str3 = @"&count=10&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    NSString *str = [[[[str1 stringByAppendingString:self.good_id] stringByAppendingString:str2] stringByAppendingString:[NSString stringWithFormat:@"%ld", self.page]] stringByAppendingString:str3];
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSArray *array = result[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            CommentModel *comment = [CommentModel modelWithDic:dic];
            [self.commentArray addObject:comment];
            [self.commentTableView reloadData];
        }
        self.page = self.page + 1;

    } andFailureBlock:^(NSError *error) {
    }];
}

- (void)createBackButton {
//    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    backButton.frame = CGRectMake(0, 0, 40, 40);
//    [backButton setImage:[UIImage imageNamed:@"25"] forState:UIControlStateNormal];
//    [backButton addTarget:self action:@selector(backClick:)forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    UIButton *left = [TextView createButtonWithImage:[UIImage imageNamed:@"10"]];
    [left addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    
//    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    shareButton.frame = CGRectMake(0, 0, 20, 20);
//    [shareButton setImage:[UIImage imageNamed:@"31"] forState:UIControlStateNormal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
}

- (void)backClick:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
